<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row>
				<div class="flex justify-between w-full mb8">
					<el-space>
						<el-popover trigger="click" :width="360" placement="bottom-start">
							<el-form :inline="false" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef" label-width="60px" size="small">
								<el-row>
									<el-col :span="24" class="mb-2">
										<el-form-item label="消费类型" prop="usedType">
											<el-select clearable v-model="state.queryForm.usedType" :teleported="false">
												<el-option label="配送" :value="10" />
												<!-- <el-option label="代付" :value="20" /> -->
												<el-option label="出库配送" :value="30" />
												<!-- <el-option label="出库代付" :value="40" /> -->
												<el-option label="正常消费" :value="50" />
												<el-option label="设备配送" :value="60" />
												<!-- <el-option label="设备代付" :value="70" /> -->
											</el-select>
										</el-form-item>
									</el-col>
									<el-col :span="24" class="mb-2">
										<el-form-item label="时间" prop="createTime">
											<el-date-picker
												:end-placeholder="$t('syslog.inputEndPlaceholderTip')"
												:start-placeholder="$t('syslog.inputStartPlaceholderTip')"
												range-separator="To"
												type="datetimerange"
												v-model="state.queryForm.createTime"
												value-format="YYYY-MM-DD HH:mm:ss"
												:teleported="false"
											/>
										</el-form-item>
									</el-col>
								</el-row>
							</el-form>
							<template #reference>
								<el-button link type="primary"
									>高级搜索<el-icon><ArrowDown /></el-icon>
								</el-button>
							</template>
						</el-popover>
						<el-input placeholder="请输入单位名称" v-model="state.queryForm.deptName" />
						<el-button type="primary" @click="getDataList(true)">搜索</el-button>
						<el-button icon="refresh" @click="resetQuery()"></el-button>
					</el-space>
					<div>
						<el-button v-auth="'platform_basicConsumptionRecord_export'" type="primary" class="mr-2" icon="" @click="exportExcel">导出</el-button>
					</div>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<!-- <el-table-column type="selection" width="40" align="center" /> -->
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="callPhone" label="呼叫号码" show-overflow-tooltip width="120" />
				<el-table-column prop="callTime" label="呼叫时间" show-overflow-tooltip width="140" />
				<el-table-column prop="usedType" label="消费类型" show-overflow-tooltip width="90">
					<template #default="{ row }">
						<div>
							<span v-if="row.usedType === 10">配送</span>
							<!-- <span v-if="row.usedType === 20">代付</span> -->
							<span v-if="row.usedType === 30">出库配送</span>
							<!-- <span v-if="row.usedType === 40">出库代付</span> -->
							<span v-if="row.usedType === 50">正常消费</span>
							<span v-if="row.usedType === 60">设备配送</span>
							<!-- <span v-if="row.usedType === 70">设备代付</span> -->
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="imei" label="IMEI" show-overflow-tooltip width="150" />
				<el-table-column prop="deviceType" label="类型-型号" show-overflow-tooltip width="220">
					<template #default="{ row }">
						<span>{{ row.deviceType }}</span>
						<span class="px-1">-</span>
						<span>{{ row.deviceModelName }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="customerDeptName" label="所属单位" show-overflow-tooltip min-width="110" />
				<el-table-column prop="notificationType" label="通知类型" show-overflow-tooltip min-width="90">
					<template #default="{ row }">
						<div>
							<el-tag type="warning" v-if="row.notificationType === 10">短信</el-tag>
							<el-tag type="success" v-if="row.notificationType === 20">语音</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="alarmTime" label="报警时间" show-overflow-tooltip width="140" />
				<el-table-column prop="receiver" label="接警人" show-overflow-tooltip min-width="90" />
				<el-table-column prop="receivePhone" label="接收号码" show-overflow-tooltip width="120" />
				<el-table-column prop="smsNumber" label="短信数量" show-overflow-tooltip min-width="90">
					<template #default="{ row }">
						<span>{{ row.smsNumber === 999999 ? '无限制' : row.smsNumber }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="ttsNumber" label="语音数量(min)" show-overflow-tooltip min-width="120">
					<template #default="{ row }">
						<span>{{ row.ttsNumber === 999999 ? '无限制' : row.ttsNumber }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="ttsNumber" label="通话时长(min)" show-overflow-tooltip min-width="120">
					<template #default="{ row }">
						<span>{{ row.ttsNumber === 999999 ? '无限制' : row.ttsNumber }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="consumeType" label="消耗类型" show-overflow-tooltip min-width="90">
					<template #default="{ row }">
						<div>
							<span v-if="row.consumeType === 10">上级单位</span>
							<span v-if="row.consumeType === 20">用户自己</span>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="sendState" label="通知状态" show-overflow-tooltip min-width="90">
					<template #default="{ row }">
						<div>
							<el-tag type="success" v-if="row.sendState === 10">成功</el-tag>
							<el-tag type="danger" v-if="row.sendState === 20">失败</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="sendResults" label="通知结果" show-overflow-tooltip width="120">
					<template #default="{ row }">
						<div>
							<el-tag type="success" v-if="row.sendResults === 10">用户接收成功</el-tag>
							<el-tag type="danger" v-if="row.sendResults === 20">用户接收失败</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="记录创建时间" show-overflow-tooltip width="160" />
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增  -->
		<!-- <form-dialog ref="formDialogRef" @refresh="getDataList(false)" /> -->
	</div>
</template>

<script setup lang="ts" name="systemBasicbasicConsumptionRecord">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/tariff/basicConsumptionRecord';
import { useMessage, useMessageBox } from '/@/hooks/message';

// 引入组件
// const FormDialog = defineAsyncComponent(() => import('./form.vue'));
// 定义查询字典

// 定义变量内容
const formDialogRef = ref();
// 搜索变量
const queryRef = ref();
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {},
	pageList: fetchList,
	descs: ['create_time'],
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	state.queryForm.deptName = '';
	getDataList();
};

// 导出excel
const exportExcel = () => {
	downBlobFile('/platform/basicConsumptionRecord/export', Object.assign(state.queryForm), '短信语音消费记录.xlsx');
};

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>
